package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserHappyFuneral;
import com.lantosec.hmrp.modules.archives.mapper.UserHappyFuneralMapper;
import com.lantosec.hmrp.modules.archives.service.IUserHappyFuneralService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserHappyFuneralGiftVO;
import com.lantosec.hmrp.modules.archives.vo.UserHappyFuneralVO;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 领导干部参与操办本人及近亲属婚丧喜庆事宜情况报告表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-14
 */
@Service
@Primary
@Log4j2
public class UserHappyFuneralServiceImpl extends ServiceImpl<UserHappyFuneralMapper, UserHappyFuneral> implements IUserHappyFuneralService {
    @Autowired
    private ISysDictionariesService iSysDictionariesService;

    @Override
    @Transactional
    public void saveUserHappyFuneral(UserHappyFuneralVO vo) {
        UserHappyFuneral userHappyFuneral = vo.convertToEntity();
        userHappyFuneral.setUserHappyFuneralId(UUIDUtils.get());
        //获取当前用户id和名称
        userHappyFuneral.setCreateId(TokenUtil.getUserId());
        userHappyFuneral.setCreateUser(TokenUtil.getName());
        userHappyFuneral.setCreateTime(DateUtils.nowDate());
        userHappyFuneral.setVerifyStatus(1);
        this.save(userHappyFuneral);
        saveUserHappyFuneralGift(vo,userHappyFuneral.getUserHappyFuneralId());
    }

    private void saveUserHappyFuneralGift(UserHappyFuneralVO vo,String userHappyFuneralId) {
        //新增礼品表
        List<UserHappyFuneralGiftVO> userHappyFuneralGiftVO = vo.getUserHappyFuneralGiftVO();
        userHappyFuneralGiftVO.forEach(param->{
            param.setUserHappyFuneralGiftId(UUIDUtils.get());
            param.setUserHappyFuneralId(userHappyFuneralId);
            baseMapper.saveUserHappyFuneralGift(param);
        });
    }

    @Override
    @Transactional
    public void updateUserHappyFuneral(UserHappyFuneralVO vo) {
        String userHappyFuneralId = vo.getUserHappyFuneralId();
        //新增一条数据
        //saveUserHappyFuneral(vo);
        //修改这条数据的审核状态为2
        if(StringUtils.isNotEmpty(userHappyFuneralId)){
            UserHappyFuneral userHappyFuneral = vo.convertToEntity();
            userHappyFuneral.setVerifyStatus(1);
            this.updateById(userHappyFuneral);
            //新增礼品表先删除
            baseMapper.deleteUserHappyFuneralGift(userHappyFuneralId);
            saveUserHappyFuneralGift(vo,userHappyFuneralId);
        }else {
            saveUserHappyFuneral(vo);
        }
    }

    @Override
    public List<UserHappyFuneralVO> getUserHappyFuneral(String userId) {
        List<UserHappyFuneralVO> userHappyFuneralVOList = new ArrayList<>();
        List<UserHappyFuneral> userHappyFuneralList = this.list(new QueryWrapper<UserHappyFuneral>().eq(UserHappyFuneral.ColName.user_id.toString(),userId).orderByDesc(UserHappyFuneral.ColName.create_time.toString()));
        userHappyFuneralList.forEach(param->{
            UserHappyFuneralVO userHappyFuneralVO = getUserHappyFuneralVo(param);
            //查询礼品表信息
            List<UserHappyFuneralGiftVO> userHappyFuneralGiftVO = baseMapper.getUserHappyFuneralGift(param.getUserHappyFuneralId());
            userHappyFuneralVO.setUserHappyFuneralGiftVO(userHappyFuneralGiftVO);
            userHappyFuneralVOList.add(userHappyFuneralVO);
        });
        return userHappyFuneralVOList;
    }

    /**
     * 组装信息
     * @param param
     */
    private UserHappyFuneralVO getUserHappyFuneralVo(UserHappyFuneral param) {
        UserHappyFuneralVO userHappyFuneralVO = param.convertToVO();
        try {
            //查询字典表中的家庭成员称谓
            String familyAppellation = param.getFamilyAppellation();
            String familyAppellationName = iSysDictionariesService.getById(familyAppellation).getDictionariesShowName();
            userHappyFuneralVO.setFamilyAppellationName(familyAppellationName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userHappyFuneralVO;
    }

    @Override
    public List<UserHappyFuneralVO> getUserHappyFuneralVerify(String userId) {
        List<UserHappyFuneralVO> userHappyFuneralVOList = new ArrayList<>();
        List<UserHappyFuneral> userHappyFuneralList = this.list(new QueryWrapper<UserHappyFuneral>().eq(UserHappyFuneral.ColName.user_id.toString(),userId).in(UserHappyFuneral.ColName.verify_status.toString(),0,2));
        userHappyFuneralList.forEach(param->{
            UserHappyFuneralVO userHappyFuneralVO = getUserHappyFuneralVo(param);
            userHappyFuneralVOList.add(userHappyFuneralVO);
        });
        return userHappyFuneralVOList;
    }

    @Override
    public void verifyUserHappyFuneral(UserHappyFuneralVO vo) {
        UserHappyFuneral userHappyFuneral = vo.convertToEntity();
        Integer verifyStatus = userHappyFuneral.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userHappyFuneral.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userHappyFuneral.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userHappyFuneral.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userHappyFuneral.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userHappyFuneral.setVerifyId(TokenUtil.getUserId());
        userHappyFuneral.setVerifyUser(TokenUtil.getName());
        userHappyFuneral.setVerifyTime(DateUtils.nowDate());
        this.updateById(userHappyFuneral);
    }

    @Override
    public List<UserHappyFuneralVO> getUserHappyFuneralHistory(String userId) {
        List<UserHappyFuneralVO> userHappyFuneralVOList = new ArrayList<>();
        List<UserHappyFuneral> userHappyFuneralList = this.list(new QueryWrapper<UserHappyFuneral>().eq(UserHappyFuneral.ColName.user_id.toString(),userId).eq(UserHappyFuneral.ColName.verify_status.toString(),4));
        userHappyFuneralList.forEach(param->{
            UserHappyFuneralVO userHappyFuneralVO = getUserHappyFuneralVo(param);
            userHappyFuneralVOList.add(userHappyFuneralVO);
        });
        return userHappyFuneralVOList;
    }
}
